package com.zy.openchat.core.model.call;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class CallBaseInfo implements Parcelable {
    //通话标识
    public long callId;
    //频道名称
    public String channelName;
    //通话发起者userid
    public long from;
    //邀请对象，userid list
    public List<Long> to;
    //归属chat，可以为0
    public long chatId;
    //是否视频
    public boolean isVideo;
    //是否多人会议
    public boolean isMeetingAV;

    public long getRealChatId() {
        if (isMeetingAV) {
            return chatId;
        } else {
            if (from == UserInfo.getUserInfo().id) {
                return chatId;
            } else {
                return from;
            }
        }
    }

    public String done_jsonForMessage() {
        return MessageInfo.getTextExMessage(new Gson().toJson(this), TelegramManager.AudioAVideo_MessageType, TelegramManager.AudioAVideo_MessageType_Done);
    }

    public CallBaseInfo() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.callId);
        dest.writeString(this.channelName);
        dest.writeLong(this.from);
        dest.writeList(this.to);
        dest.writeLong(this.chatId);
        dest.writeByte(this.isVideo ? (byte) 1 : (byte) 0);
        dest.writeByte(this.isMeetingAV ? (byte) 1 : (byte) 0);
    }

    protected CallBaseInfo(Parcel in) {
        this.callId = in.readLong();
        this.channelName = in.readString();
        this.from = in.readLong();
        this.to = new ArrayList<Long>();
        in.readList(this.to, Long.class.getClassLoader());
        this.chatId = in.readLong();
        this.isVideo = in.readByte() != 0;
        this.isMeetingAV = in.readByte() != 0;
    }

    public static final Creator<CallBaseInfo> CREATOR = new Creator<CallBaseInfo>() {
        @Override
        public CallBaseInfo createFromParcel(Parcel source) {
            return new CallBaseInfo(source);
        }

        @Override
        public CallBaseInfo[] newArray(int size) {
            return new CallBaseInfo[size];
        }
    };
}
